#include "PMInternetFile.h"

#define dim(a) (sizeof(a)/sizeof(a[0]))

PMInternetFile::PMInternetFile(void)
{
	hInternet=NULL,
	hRequest=NULL;
	hConnect=NULL;
}

PMInternetFile::~PMInternetFile(void)
{
	Close();
}
DWORD PMInternetFile::FileSize()
{
	if(hRequest)
		return GetInternetFileSize(hRequest);
	else
		return 0;
}

DWORD PMInternetFile::SetRange(DWORD start,DWORD end)
{
	if(hRequest)
		return SetInternetFileRange(hRequest,start,end);
	else
		return 0;
}
DWORD PMInternetFile::SetTimeOut(DWORD value)
{
	DWORD dwtimeout,dwlen=sizeof(dwtimeout);
	if(hInternet)
	{
		if(InternetQueryOption(hInternet,INTERNET_OPTION_CONNECT_TIMEOUT,&dwtimeout,&dwlen))
		{
			dwtimeout=value;
			if(InternetSetOption(hInternet,INTERNET_OPTION_CONNECT_TIMEOUT,&dwtimeout,dwlen))
				return TRUE;
		}
	}
	return FALSE;
}
DWORD PMInternetFile::Open(LPCTSTR pURL)
{
	hInternet=InternetOpen(_T("power"),INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
	if(hInternet)
	{
		//this function will take a long time
		hRequest=InternetOpenUrl(hInternet,pURL,NULL,
			0,INTERNET_FLAG_RELOAD,0);
		if(hRequest)
		{
			SetRange(20,25);
			SetTimeOut(60000);
			return TRUE;
		}
	}
	else
		InternetCloseHandle(hInternet);
	return FALSE;
}
DWORD PMInternetFile::OpenEx(LPCTSTR pURL,DWORD start,DWORD end)
{
	HINTERNET hConnect;
	DWORD nRet;
	LPTSTR AcceptTypes[2] = {TEXT("*/*"), NULL}; 
	DWORD dwSize = 0;
	URL_COMPONENTS URLinfo;
	TCHAR hostname[256];
	TCHAR URLpath[256];
	TCHAR AddHeader[200];

	memset(&URLinfo,0,sizeof(URLinfo));
	URLinfo.dwStructSize=sizeof(URLinfo);
	URLinfo.lpszHostName=hostname;
	URLinfo.dwHostNameLength=dim(hostname);
	URLinfo.lpszUrlPath=URLpath;
	URLinfo.dwUrlPathLength=dim(URLpath);
	InternetCrackUrl(pURL,wcslen(pURL),ICU_DECODE,&URLinfo);

	hInternet=InternetOpen(_T("power"),INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
	if(hInternet)
	{
		// Open an HTTP session for a specified site by using lpszServer. 
		if ((hConnect = InternetConnect (hInternet,hostname,URLinfo.nPort, 
										  NULL, NULL,INTERNET_SERVICE_HTTP,0, 0)))
		{
			// Open an HTTP request handle. 
			if ((hRequest = HttpOpenRequest (hConnect,NULL,URLpath,HTTP_VERSION,NULL, 
											  (LPCTSTR *)AcceptTypes,INTERNET_FLAG_RELOAD, 0)))
			{
				//prepare HTTP addition header
				memset(AddHeader,0,sizeof(AddHeader));
				if(end==0)
				{
					wsprintf(AddHeader,_T("Range:bytes=%d-"),start);
				}
				else
				{
					if(end>start)
						wsprintf(AddHeader,_T("Range:bytes=%d-%d"),start,end);
				}
				// Send a request to the HTTP server. 
				if (HttpSendRequest(hRequest,AddHeader,_wtoi(AddHeader), NULL, 0))
				{
					SetRange(20,25);
					return TRUE;
				}
				else
					nRet=GetLastError();

			}
		}
	}
	else
		InternetCloseHandle(hInternet);
	return FALSE;
}
DWORD PMInternetFile::Close(void)
{
    if(hRequest)
	{
        InternetCloseHandle(hRequest);
		hRequest=NULL;
	}
	if(hConnect)
	{
		InternetCloseHandle(hConnect);
		hConnect=NULL;
	}
	if(hInternet)
	{
		InternetCloseHandle(hInternet);
		hInternet=NULL;
	}
	return TRUE;
}


DWORD PMInternetFile::Read(LPVOID buf,DWORD bufsize,DWORD * pdwread)
{
#if 0
	INTERNET_BUFFERS i_buffer;
	BOOL bRet;
	memset(&i_buffer,0,sizeof(i_buffer));
	i_buffer.dwStructSize=sizeof(i_buffer);
	if(InternetReadFileEx(hRequest,&i_buffer,IRF_NO_WAIT,0))
	{
		memcpy(buf,i_buffer.lpvBuffer,i_buffer.dwBufferTotal);
		return TRUE;
	}
	else
	{
		bRet=GetLastError();
		return FALSE;
	}
#endif
	return InternetReadFile(hRequest,buf,bufsize,pdwread);
}

